7b548611276d71de77481dbafb1d9fe55965e1e1
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 namespace MediaWiki\Logger
;
27 * LoggerFactory service provider that creates loggers implemented by
30 * Configured using an array of configuration data with the keys 'loggers',
31 * 'processors', 'handlers' and 'formatters'.
33 * The ['loggers']['\@default'] configuration will be used to create loggers
34 * for any channel that isn't explicitly named in the 'loggers' configuration
37 * Configuration will most typically be provided in the $wgMWLoggerDefaultSpi
38 * global configuration variable used by LoggerFactory to construct its
39 * default SPI provider:
41 * $wgMWLoggerDefaultSpi = array(
42 * 'class' => '\\MediaWiki\\Logger\\MonologSpi',
43 * 'args' => array( array(
45 * '@default' => array(
46 * 'processors' => array( 'wiki', 'psr', 'pid', 'uid', 'web' ),
47 * 'handlers' => array( 'stream' ),
50 * 'processors' => array( 'wiki', 'psr', 'pid' ),
51 * 'handlers' => array( 'stream' ),
54 * 'processors' => array(
56 * 'class' => '\\MediaWiki\\Logger\\Monolog\\WikiProcessor',
59 * 'class' => '\\Monolog\\Processor\\PsrLogMessageProcessor',
62 * 'class' => '\\Monolog\\Processor\\ProcessIdProcessor',
65 * 'class' => '\\Monolog\\Processor\\UidProcessor',
68 * 'class' => '\\Monolog\\Processor\\WebProcessor',
71 * 'handlers' => array(
73 * 'class' => '\\Monolog\\Handler\\StreamHandler',
74 * 'args' => array( 'path/to/your.log' ),
75 * 'formatter' => 'line',
78 * 'class' => '\\Monolog\\Handler\\RedisHandler',
79 * 'args' => array( function() {
80 * $redis = new Redis();
81 * $redis->connect( '127.0.0.1', 6379 );
86 * 'formatter' => 'logstash',
89 * 'class' => '\\MediaWiki\\Logger\\Monolog\\LegacyHandler',
91 * 'udp://127.0.0.1:8420/mediawiki
93 * 'formatter' => 'line',
96 * 'formatters' => array(
98 * 'class' => '\\Monolog\\Formatter\\LineFormatter',
100 * 'logstash' => array(
101 * 'class' => '\\Monolog\\Formatter\\LogstashFormatter',
102 * 'args' => array( 'mediawiki', php_uname( 'n' ), null, '', 1 ),
109 * @see https://github.com/Seldaek/monolog
111 * @author Bryan Davis <bd808@wikimedia.org>
112 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
114 class MonologSpi
implements Spi
{
117 * @var array $singletons
119 protected $singletons;
122 * Configuration for creating new loggers.
129 * @param array $config Configuration data.
131 public function __construct( array $config ) {
132 $this->config
= array();
133 $this->mergeConfig( $config );
138 * Merge additional configuration data into the configuration.
141 * @param array $config Configuration data.
143 public function mergeConfig( array $config ) {
144 foreach ( $config as $key => $value ) {
145 if ( isset( $this->config
[$key] ) ) {
146 $this->config
[$key] = array_merge( $this->config
[$key], $value );
148 $this->config
[$key] = $value;
156 * Reset internal caches.
158 * This is public for use in unit tests. Under normal operation there should
159 * be no need to flush the caches.
161 public function reset() {
162 $this->singletons
= array(
163 'loggers' => array(),
164 'handlers' => array(),
165 'formatters' => array(),
166 'processors' => array(),
172 * Get a logger instance.
174 * Creates and caches a logger instance based on configuration found in the
175 * $wgMWLoggerMonologSpiConfig global. Subsequent request for the same channel
176 * name will return the cached instance.
178 * @param string $channel Logging channel
179 * @return \\Psr\\Log\\LoggerInterface Logger instance
181 public function getLogger( $channel ) {
182 if ( !isset( $this->singletons
['loggers'][$channel] ) ) {
183 // Fallback to using the '@default' configuration if an explict
184 // configuration for the requested channel isn't found.
185 $spec = isset( $this->config
['loggers'][$channel] ) ?
186 $this->config
['loggers'][$channel] :
187 $this->config
['loggers']['@default'];
189 $monolog = $this->createLogger( $channel, $spec );
190 $this->singletons
['loggers'][$channel] = $monolog;
193 return $this->singletons
['loggers'][$channel];
199 * @param string $channel Logger channel
200 * @param array $spec Configuration
201 * @return \\Monolog\\Logger
203 protected function createLogger( $channel, $spec ) {
204 $obj = new Logger( $channel );
206 if ( isset( $spec['processors'] ) ) {
207 foreach ( $spec['processors'] as $processor ) {
208 $obj->pushProcessor( $this->getProcessor( $processor ) );
212 if ( isset( $spec['handlers'] ) ) {
213 foreach ( $spec['handlers'] as $handler ) {
214 $obj->pushHandler( $this->getHandler( $handler ) );
222 * Create or return cached processor.
223 * @param string $name Processor name
226 public function getProcessor( $name ) {
227 if ( !isset( $this->singletons
['processors'][$name] ) ) {
228 $spec = $this->config
['processors'][$name];
229 $processor = ObjectFactory
::getObjectFromSpec( $spec );
230 $this->singletons
['processors'][$name] = $processor;
232 return $this->singletons
['processors'][$name];
237 * Create or return cached handler.
238 * @param string $name Processor name
239 * @return \\Monolog\\Handler\\HandlerInterface
241 public function getHandler( $name ) {
242 if ( !isset( $this->singletons
['handlers'][$name] ) ) {
243 $spec = $this->config
['handlers'][$name];
244 $handler = ObjectFactory
::getObjectFromSpec( $spec );
245 if ( isset( $spec['formatter'] ) ) {
246 $handler->setFormatter(
247 $this->getFormatter( $spec['formatter'] )
250 $this->singletons
['handlers'][$name] = $handler;
252 return $this->singletons
['handlers'][$name];
257 * Create or return cached formatter.
258 * @param string $name Formatter name
259 * @return \\Monolog\\Formatter\\FormatterInterface
261 public function getFormatter( $name ) {
262 if ( !isset( $this->singletons
['formatters'][$name] ) ) {
263 $spec = $this->config
['formatters'][$name];
264 $formatter = ObjectFactory
::getObjectFromSpec( $spec );
265 $this->singletons
['formatters'][$name] = $formatter;
267 return $this->singletons
['formatters'][$name];